package th;

/**
 * @author Mark
 * @version 1.0
 * @date 2020/5/17 23:04
 * @description 龟兔赛跑
 */
public class RunWork implements Runnable {

    private static String winner;

    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if ("兔子".equals(Thread.currentThread().getName()) && i % 10 == 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            boolean flag = winner(i);
            if (flag) {
                break;
            }
            System.out.println(Thread.currentThread().getName() + "-->跑了" + i + "步");
        }
    }

    private boolean winner(int steps) {
        if (winner != null) {
            return true;
        }
        if (steps >= 100) {
            winner = Thread.currentThread().getName();
            System.out.println(winner + "is winner");
            return true;
        }
        return false;

    }

    public static void main(String[] args) {
        RunWork run = new RunWork();
        new Thread(run, "乌龟").start();
        new Thread(run, "兔子").start();

    }
}
